<?php
// @formatter:off
/**
 * @file c.edit-ALSO.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-04 03:02:53 +0000 (Tue, 04 Jun 2013) $
 *
 * $Id: c.edit-ALSO.php 68 2013-06-04 03:02:53Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/extensions/TagsRelator/controlers/c.edit-ALSO.php $
 */
// @formatter:on

global $wcTagsHolder;

$targetName = explode(",", $_POST["ACTION_TARGET"]);
$targetName = array_shift($targetName);
trim($targetName);

if($_POST["EDITACTION"] == "ADD" && $targetName != $wcRuleAction->target) {
	$tag = $wcTagsHolder->itemByName($targetName, $wcGroup);
	if($tag && $tag->ok()) {
		$wcRuleAction->target = $tag->name;
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[ALSO-TARGET]";
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "update {$dbprefix}acts_tare\n";
			$query .= "set    act_tag = :tag\n";
			$query .= "where  act_rule   = :rule\n";
			$query .= " and   act_group  = :group\n";
			$query .= " and   act_action = :action\n";

			$stmt = $db->prepare($stmtId, $query);
		}

		$params = array(
			":rule" => $wcRule->id(),
			":group" => $wcGroup->id,
			":action" => $wcRuleAction->id(),
			":tag" => $tag->id,
		);

		$stmt->execute($params);

		$wcRuleAction->reload();
	}

}

switch($_POST["EDITACTION"]) {
	case "ADD":
		if($wcRuleAction->target) {
			$newTagNames = array();
			foreach(explode(",",$_POST["ACTION_TAGS"]) as $tagName) {
				$tagName = trim($tagName);
				if($tagName) {
					$newTagNames[] = $tagName;
				}
			}

			$newTags = array();
			$currentIds = array_keys($wcRuleAction->tagsAlso());
			foreach($newTagNames as $name) {
				$tag = $wcTagsHolder->itemByName($name, $wcGroup);

				//
				// If it is a new tag it creates it.
				if(!$tag) {
					$wcTagsHolder->create($name, $wcGroup);
					$tag = $wcTagsHolder->itemByName($name, $wcGroup);
				}

				if($tag && $tag->ok() && !in_array($tag->id, $currentIds)) {
					$newTags[$tag->id] = $tag;
				}
			}

			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__."[ALSO-ADD]";
			if($db->has($stmtId)) {
				$stmt = $db->get($stmtId);
			} else {
				$query = "insert\n";
				$query .= "        into {$dbprefix}acts_tare (\n";
				$query .= "                act_rule, act_group, act_action, act_tag, act_also)\n";
				$query .= "        values (:rule, :group, :action, :tag, :also)\n";

				$stmt = $db->prepare($stmtId, $query);
			}

			$tag = $wcTagsHolder->itemByName($wcRuleAction->target, $wcGroup);
			if($tag && $tag->ok()) {
				foreach($newTags as $also) {
					$params = array(
						":rule" => $wcRule->id(),
						":group" => $wcGroup->id,
						":action" => $wcRuleAction->id(),
						":tag" => $tag->id,
						":also" => $also->id
					);

					$stmt->execute($params);
				}

				$out = true;
			}
		}
		$wcRuleAction->reload();

		break;
	case "REMOVE":
		$tagIds = array();
		foreach($_POST as $name => $value) {
			if(substr($name, 0, 4) == "TAG_") {
				$tagIds[] = substr($name, 4);
			}
		}

		$badTags = array();
		$currentIds = array_keys($wcRuleAction->tagsAlso());
		foreach($tagIds as $id) {
			$tag = $wcTagsHolder->item($id, $wcGroup);
			if($tag->ok() && in_array($tag->id, $currentIds)) {
				$badTags[$tag->id] = $tag;
			}
		}

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[ALSO-REMOVE]";
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "delete from {$dbprefix}acts_tare\n";
			$query .= "where       act_rule   = :rule\n";
			$query .= " and        act_group  = :group\n";
			$query .= " and        act_action = :action\n";
			$query .= " and        act_also   = :also\n";

			$stmt = $db->prepare($stmtId, $query);
		}

		foreach($badTags as $also) {
			$params = array(
				":rule" => $wcRule->id(),
				":group" => $wcGroup->id,
				":action" => $wcRuleAction->id(),
				":also" => $also->id
			);

			$stmt->execute($params);
		}

		$wcRuleAction->reload();

		break;
	default:
		break;
}
?>